package com.iterator.service.wechat.lyh;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.lyh.utils.DataCountDataVO;
import com.iterator.bean.web.pojo.zql.Coupon;
import com.iterator.bean.web.pojo.zql.CouponExample;
import com.iterator.bean.web.pojo.zql.CouponUser;
import com.iterator.bean.wechat.lyh.WxCouponMyVO;
import com.iterator.bean.wechat.lyh.WxCouponVO;
import com.iterator.mapper.web.promotionMapper.CouponMapper;
import com.iterator.mapper.wechat.lyh.WxCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@EnableScheduling
@Service
public class WxCouponServiceImpl implements WxCouponService {

    @Autowired
    WxCouponMapper couponMapper;

    @Autowired
    CouponMapper couponMapper2;

    //定时任务
    //每隔5分钟执行一次
    @Scheduled(cron = "0 0/5 * * * ?")
    public void timingRefreshInvalidCoupon(){
        //定时刷新失效优惠卷
        couponMapper.timingRefreshInvalidCoupon();
        //定时刷新失效用户优惠卷
        couponMapper.timingRefreshInvalidUserCoupon();
        //定时刷新失效的用户订单
        couponMapper.timingRefreshInvalidUserOrder();
    }

    @Override
    public DataCountDataVO getCouponList(Integer page, Integer size) {
        //设置分页
        PageHelper.startPage(page, size);
        //查询语句,Status是0(1是已过期,2是已下架)，Deleted是false
        List<WxCouponVO> couponVOS =
                couponMapper.selectCouponListByStatusAndDeleted(0, false);
        //分页前的原数据量
        PageInfo<WxCouponVO> pageInfo = new PageInfo<>(couponVOS);
        long total = pageInfo.getTotal();

        DataCountDataVO dataVO = new DataCountDataVO();
        dataVO.setData(couponVOS);
        dataVO.setCount(total);

        return dataVO;
    }

    @Override
    public DataCountDataVO getMyCouponList(String username, Integer status, Integer page, Integer size) {
        //设置分页
        PageHelper.startPage(page, size);
        //查询语句,status是状态码(0可用,1已用,2过期,3下架),Deleted是false
        List<WxCouponMyVO> couponVOS =
                couponMapper.selectMyCouponLByStatusAndDeleted(username, status, false);
        //分页前的原数据量
        PageInfo<WxCouponMyVO> pageInfo = new PageInfo<>(couponVOS);
        long total = pageInfo.getTotal();

        DataCountDataVO dataVO = new DataCountDataVO();
        dataVO.setData(couponVOS);
        dataVO.setCount(total);

        return dataVO;
    }

    //grouponRulesId暂时不使用
    @Override
    public List<WxCouponMyVO> selectList(String username, Integer cartId, Integer grouponRulesId) {
        return couponMapper.selectByUsernameCartIdGrouponRulesId(username, cartId, grouponRulesId, false);
    }

    @Override
    public Integer receiveCoupon(String username, Integer couponId) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        //第一个where条件
        criteria.andIdEqualTo(couponId);
        //第二个where条件
        criteria.andTypeEqualTo((short) 0);
        //第三个where条件
        criteria.andStatusEqualTo((short) 0);
        //第三个where条件
        criteria.andDeletedEqualTo(false);
        //查询数据库中是否有符合条件的优惠券
        List<Coupon> coupons = couponMapper2.selectByExample(couponExample);
        //实际上coupons == null没有符合条件的优惠券,total=-1,领取完毕
        if (coupons == null || coupons.size() == 0 || coupons.get(0).getTotal() < 0) {
            return null;
        }
        //如果coupons.get(0).getLimit()=0,说明该优惠券不限量,可以不必再去查询用户领取了多少张
        if (coupons.get(0).getLimit() != 0) {
            //查询用户有几张该优惠券
            Integer intNum = couponMapper.selectUserReceiveCouponNumber(username, couponId);
            if (intNum >= coupons.get(0).getLimit()) return 0;
        }
        //返回null和0只是标识符,不必在意具体的值


        //真正开始办事

        //coupons.get(0).getLimit()为1的时候,就是该优惠券只剩下最后一个,应该把total改成-1
        //等于0是不限量,所以不用修改
        if (coupons.get(0).getTotal() == 1) {
            coupons.get(0).setTotal(-1);
            couponMapper2.updateByPrimaryKey(coupons.get(0));
        } else if (coupons.get(0).getTotal() != 0) {
            coupons.get(0).setTotal(coupons.get(0).getTotal() - 1);
            couponMapper2.updateByPrimaryKey(coupons.get(0));
        }
        // 更新coupon_user表
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(couponMapper.selectUserIdByUsername(username));
        couponUser.setCouponId(coupons.get(0).getId());
        //0则是领取时间开始,往后数coupons.get(0).getDays()天
        //1则是直接用优惠券的startTime和endTime
        if (coupons.get(0).getTimeType() == 0) {
            //用status属性先暂存优惠券有效天数,之后insert时候从这里取值
            couponUser.setStatus(coupons.get(0).getDays());
            couponMapper.insertUserCoupon1(couponUser);
        } else {
            couponUser.setStartTime(coupons.get(0).getStartTime());
            couponUser.setEndTime(coupons.get(0).getEndTime());
            couponMapper.insertUserCoupon2(couponUser);
        }
        //返回值是标识符不重要
        return 1;
    }

    @Override
    public Integer exchangeCoupon(String username, String code) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        //第一个where条件
        criteria.andCodeEqualTo(code);
        //第二个where条件
        criteria.andTypeEqualTo((short) 2);
        //第三个where条件
        criteria.andStatusEqualTo((short) 0);
        //第三个where条件
        criteria.andDeletedEqualTo(false);
        //查询数据库中是否有符合条件的优惠券
        List<Coupon> coupons = couponMapper2.selectByExample(couponExample);
        //实际上coupons == null没有符合条件的优惠券,total=-1,领取完毕
        if (coupons == null || coupons.size() == 0 || coupons.get(0).getTotal() < 0) {
            return null;
        }
        //如果coupons.get(0).getLimit()=0,说明该优惠券不限量,可以不必再去查询用户领取了多少张
        if (coupons.get(0).getLimit() != 0) {
            //查询用户有几张该优惠券
            Integer intNum = couponMapper.selectUserReceiveCouponNumber(username, coupons.get(0).getId());
            if (intNum >= coupons.get(0).getLimit()) return 0;
        }
        //返回null和0只是标识符,不必在意具体的值


        //真正开始办事

        //coupons.get(0).getLimit()为1的时候,就是该优惠券只剩下最后一个,应该把total改成-1
        //等于0是不限量,所以不用修改
        if (coupons.get(0).getTotal() == 1) {
            coupons.get(0).setTotal(-1);
            couponMapper2.updateByPrimaryKey(coupons.get(0));
        } else if (coupons.get(0).getTotal() != 0) {
            coupons.get(0).setTotal(coupons.get(0).getTotal() - 1);
            couponMapper2.updateByPrimaryKey(coupons.get(0));
        }
        // 更新coupon_user表
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(couponMapper.selectUserIdByUsername(username));
        couponUser.setCouponId(coupons.get(0).getId());
        //0则是领取时间开始,往后数coupons.get(0).getDays()天
        //1则是直接用优惠券的startTime和endTime
        if (coupons.get(0).getTimeType() == 0) {
            //用status属性先暂存优惠券有效天数,之后insert时候从这里取值
            couponUser.setStatus(coupons.get(0).getDays());
            couponMapper.insertUserCoupon1(couponUser);
        } else {
            couponUser.setStartTime(coupons.get(0).getStartTime());
            couponUser.setEndTime(coupons.get(0).getEndTime());
            couponMapper.insertUserCoupon2(couponUser);
        }
        //返回值是标识符不重要
        return 1;
    }
}
